package uns.fit.gis.mapdrawer;

import javax.microedition.lcdui.Graphics;

import uns.fit.gis.Util;
import uns.fit.gis.mapobj.Point;

public class DirectionArrowDrawer {
	public static void draw(Graphics g, int x0, int y0, int x1, int y1, int x2, int y2, int width) {
		//MapCanvas.drawLine(g, x0, y0, x1, y1, width);
		//MapCanvas.drawLine(g, x1, y1, x1 + (x2 - x1) / 2, y1 + (y2 - y1) / 2, width);
		
		//find 2 remaining points of arrow
		Point center = new Point(x2, y2);
		Point point = new Point(x1 + (x2 - x1) / 2, y1 + (y2 - y1) / 2);
		point = MapRotation.rotate(point, center, 30);
		int x3 = point.getX();
		int y3 = point.getY();
		point = MapRotation.rotate(point, center, -60);
		int x4 = point.getX();
		int y4 = point.getY();
		
		//g.fillTriangle(x2, y2, x3, y3, x4, y4);
	}
	
	public static void draw(Graphics g, int x0, int y0, int x1, int y1, int width) {
		//find 2 remaining points of arrow
		Point center = new Point(x1, y1);
		Point point = new Point(x0 + (x1 - x0) / 2, y0 + (y1 - y0) / 2);
		int angle = getAngle(width, x0, y0, x1, y1);
		//angle = 10;
		point = MapRotation.rotate(point, center, angle);
		int x2 = point.getX();
		int y2 = point.getY();
		point = MapRotation.rotate(point, center, -angle - angle);
		int x3 = point.getX();
		int y3 = point.getY();
		
		//MapCanvas.drawLine(g, x0, y0, x2 + (x3 - x2) / 2, y2 + (y3 - y2) / 2, width);
		
		//g.fillTriangle(x1, y1, x2, y2, x3, y3);
	}
	
	private static int getAngle(int width, int x0, int y0, int x1, int y1) {
		// Goc nay khong the bang 90 do
		if(x0 == x1)
			return 90;
		int tanX1000 = (int)(2000 * width / Util.calculateDistancePoint2Point(x0, y0, x1, y1));
		//System.out.println("Tan = " + tanX1000);
		int angle = 0;
		if(tanX1000 >  2747)
			angle = 80;
		else if(tanX1000 > 1732)
			angle = 70;
		else if(tanX1000 > 1192)
			angle = 60;
		else if(tanX1000 > 839)
			angle = 50;
		else if(tanX1000 > 577)
			angle = 40;
		else if(tanX1000 > 364)
			angle = 30;
		else if (tanX1000 > 176)
			angle = 20;
		else if(tanX1000 > 0)
			angle = 10;
		//System.out.println("Angle = " + angle);
		return angle;
	}
}
